﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class SysMessageService : BaseAppService<SysMessageDto, SysMessage, SysMessageRepository>
    {
        readonly ExcelHelp _excelHelp;
        public SysMessageService(SysMessageRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<SysMessage, SysMessageDto> GetLoadPageOffsetMode(PagingModel<SysMessageDto> pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     //.Add("SendTime", "=")
     //.Add("FromUser", "=")
     .Add("ToUser", "=")
            //.Add("BusiType", "=")
            //.Add("BusiTable", "=")
            //.Add("BusiID", "=")
            .Add("Content", "in");
            //.Add("IsRead", "=");


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            //loadPageOffsetModel.Selector = model => new SysMessage()
            //{
            //    Id = model.Id,
            //    SendTime = model.SendTime,
            //    FromUser = model.FromUser,
            //    ToUser = model.ToUser,
            //    BusiType = model.BusiType,
            //    BusiTable = model.BusiTable,
            //    BusiID = model.BusiID,
            //    Content = model.Content,
            //    IsRead = model.IsRead,
            //};
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<SysMessageDto>> GetList(PagingModel<SysMessageDto> pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        /// <summary>
        /// 获取指定用户未读消息数量
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<int> GetUnreadMsgCountAsync(Guid userId)
        {
            return await _repository.QueryBase().Where(msg => msg.ToUser == userId && msg.IsRead == false).CountAsync();
        }


        /// <summary>
        /// 获取指定用户未读消息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<SysMessage>> GetUnreadMsgAsync(Guid userId)
        {
            return await _repository.QueryBase().Where(msg => msg.ToUser == userId && msg.IsRead == false).OrderByDescending(a => a.SendTime).ToListAsync();
        }


        /// <summary>
        /// 设置消息已读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task SetReadAsync(Guid id)
        {
            var entity = await _repository.GetAsync(id);
            entity.IsRead = true;
            await _repository.SaveAsync();
        }
    }

}
